package nc.http.basic.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.pub.util.BaseDaoUtil;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.org.PostVO;
import nc.vo.pub.lang.UFBoolean;


public class SyncingOrganizationalFramework {

	public JSONObject syncOrgElementsBaseInfo(List<Map<String, Object>> list) throws Exception {

		JSONObject dates = new JSONObject();
		dates.put("appName", "bip");
		//dates.put("orgSyncConfig", "");
		dates.put("deleteNoInOrgJsonData", false);

		JSONArray orgJsonDatas = new JSONArray();

		for (Map<String, Object> deptVO : list) {
			JSONObject orgJsonData = new JSONObject();

			orgJsonData.put("id", deptVO.get("id"));
			orgJsonData.put("lunid", deptVO.get("lunid"));
			orgJsonData.put("name", deptVO.get("name"));
			orgJsonData.put("no", deptVO.get("no"));
			orgJsonData.put("isAvailable", deptVO.get("isavailable"));
			orgJsonData.put("type", deptVO.get("type"));
			orgJsonDatas.add(orgJsonData);
		}

		dates.put("orgJsonData", orgJsonDatas.toJSONString());

		return dates;
	}

	public JSONObject syncOrgElementsPsn(List<Map<String, Object>> list) throws Exception {

		JSONObject dates = new JSONObject();
		dates.put("appName", "bip");
		dates.put("orgSyncConfig", "{\"person\": [\"no\",  \"name\", \"loginName\", \"email\", \"mobileNo\", \"parent\", \"workPhone\", \"sex\"]"
				+ "}");

		JSONArray orgJsonDatas = new JSONArray();

		for (Map<String, Object> deptVO : list) {
			JSONObject orgJsonData = new JSONObject();

			orgJsonData.put("id", deptVO.get("id"));
			orgJsonData.put("lunid", deptVO.get("lunid"));
			orgJsonData.put("name", deptVO.get("name"));
			orgJsonData.put("no", deptVO.get("no"));
			orgJsonData.put("isAvailable", (new UFBoolean((String) deptVO.get("isavailable"))).booleanValue()); //�Ƿ���Ч,�����Ծ�������֯�ܹ��Ƿ�ɾ����������Ϊ��
			orgJsonData.put("type", deptVO.get("type"));//��֯�ܹ�����
			orgJsonData.put("parent", deptVO.get("parent")); //����
			orgJsonData.put("loginName", deptVO.get("loginName")); //��¼��,����typeΪpersonʱ,�д���Ϣ
			orgJsonData.put("mobileNo", deptVO.get("mobileNo")); //�ֻ���,����typeΪpersonʱ,�д���Ϣ
			orgJsonData.put("email", deptVO.get("email"));//�ʼ���ַ,����typeΪpersonʱ,�д���Ϣ
			orgJsonData.put("workPhone", deptVO.get("workphone")); //�칫�绰,����typeΪpersonʱ,�д���Ϣ
			orgJsonData.put("sex", deptVO.get("sex")); // �Ա�


			orgJsonDatas.add(orgJsonData);
		}

		dates.put("orgJsonData", orgJsonDatas.toJSONString());

		return dates;
	}


	public JSONObject syncOrgElementsPost(List<PostVO> list) throws Exception {

		JSONObject dates = new JSONObject();
		dates.put("appName", "bip");
		dates.put("orgSyncConfig", "{\"post\": [\"no\", \"name\",\"persons\",\"parent\"]}");

		List<Map<String, Object>> lists = (List<Map<String, Object>>) BaseDaoUtil.getInstance().executeQuery("SELECT * FROM syncOrgElementsBaseInfo_post_ts_view", new MapListProcessor());

		Map<String, JSONArray> map = new HashMap<>();
		for (Map<String, Object> postVO : lists) {
			String pkPost = (String) postVO.get("pk_post");
			if (map.containsKey(pkPost)) {
				 if(postVO.get("code")!=null) {
						JSONArray jsonArray = map.get(pkPost);
						jsonArray.add(postVO.get("code"));
				 }

			} else {
				JSONArray jsonArray = new JSONArray();
				 if(postVO.get("code")!=null) {
						jsonArray.add(postVO.get("code"));
						map.put(pkPost, jsonArray); 
				 }

			}
		}


		JSONArray orgJsonDatas = new JSONArray();

		for (PostVO postVO : list) {
			JSONObject orgJsonData = new JSONObject();

			orgJsonData.put("id", postVO.getPk_post());
			orgJsonData.put("lunid", postVO.getPk_post());
			orgJsonData.put("name", postVO.getPostname());
			orgJsonData.put("no", postVO.getPostcode());
			orgJsonData.put("type", "post");
			orgJsonData.put("isAvailable", true);
			orgJsonData.put("parent", postVO.getPk_dept());
			JSONArray personsArr = map.get( postVO.getPk_post());
			if(personsArr == null){
				personsArr = new JSONArray();
				continue;
			}else {
				orgJsonData.put("persons", personsArr);
			}
			

			orgJsonDatas.add(orgJsonData);
		}

		dates.put("orgJsonData", orgJsonDatas.toJSONString());

		return dates;
	}

	public JSONObject syncOrgElementsDept(List<Map<String, Object>> list) throws Exception {

		JSONObject dates = new JSONObject();
		dates.put("appName", "bip");
		dates.put("orgSyncConfig", "{\"dept\": [\"no\",  \"name\",  \"parent\", \"thisLeader\",\"superLeader\"]}");

		JSONArray orgJsonDatas = new JSONArray();

		for (Map<String, Object> deptVO : list) {
			JSONObject orgJsonData = new JSONObject();

			orgJsonData.put("id", deptVO.get("id"));
			orgJsonData.put("lunid", deptVO.get("lunid"));
			orgJsonData.put("name", deptVO.get("name"));
			orgJsonData.put("no", deptVO.get("no"));
			orgJsonData.put("isAvailable", (new UFBoolean((String) deptVO.get("isavailable"))).booleanValue());
			orgJsonData.put("type", deptVO.get("type"));
			orgJsonData.put("parent", deptVO.get("parent"));
			orgJsonData.put("thisLeader", deptVO.get("thisleader"));
			orgJsonData.put("superLeader", deptVO.get("superleader"));

			orgJsonDatas.add(orgJsonData);
		}

		dates.put("orgJsonData", orgJsonDatas.toJSONString());

		return dates;
	}
}
